#include<iostream>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<string.h>

#define ERR_EXIT(m)\
do{\
    perror(m);\
    exit(EXIT_FAILURE);\
}while(0)
int main(){
    FILE* fp = fopen("Backup.txt","w");
    if (!fp) {
        std::cerr << "Error opening Backup.txt" << std::endl;
        return 1;
    }

    FILE* fifop=fopen("myfifo","r");
    if(!fifop){
        std::cerr<<"Error opening myfifo"<<std::endl;
        fclose(fp);
        return 1;
    }

   char buffer[100]={0};
    if(fgets(buffer,sizeof(buffer)-1,fifop)){
        std::cout<<"Writing"<<"\""<<buffer<<"\"into Backup.txt"<<std::endl;
        //不能fwrite sizeof（buffer)-1，会把后面的"\0"都写入文件导致认为文件是二进制文件
        fwrite(buffer,sizeof(char),strlen(buffer),fp);
    }
    else{
        std::cerr<<"Error getting character from fifop"<<std::endl;
        fclose(fp);
        return 1;
    }

    fclose(fifop);
    fclose(fp);
    //unlink("OriginalFile.txt");//删除原文件
    return 0;
}